package com.bjpowernode.controller;
import com.bjpowernode.beans.DictionaryValue;
import com.bjpowernode.exception.MyLengthException;
import com.bjpowernode.services.DictionaryValueServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;

@RestController
@RequestMapping("/DictionaryValue")
public class DictionaryValueController {
    @Autowired
    DictionaryValueServices dictionaryValueServices;

    @RequestMapping("getAll.action")
    public ArrayList<DictionaryValue> getAll() {
        System.out.println("aaaaaaaaaaaaaaaaaaaa");
        ArrayList<DictionaryValue> all = dictionaryValueServices.getAll();
        return all;
    }

    @RequestMapping("del.action")
    public HashMap<String, Object> del(@RequestBody String[] ids) throws SQLException {
        HashMap<String, Object> map = new HashMap<>();
        try {
            int del = dictionaryValueServices.del(ids);
            if (del > 0) {
                map.put("result", "success");
            } else {
                throw new SQLException("由于业务逻辑错误，则数据库删除影响行数异常");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            throw new SQLException("由于SQL语句错误，则我们需要再次抛出一个异常交给增强控制器，数据库删除影响行数异常");
        }
        return map;
    }


    @RequestMapping("add.action")
    public HashMap<String, Object> add(DictionaryValue dictionaryValue) throws SQLException, MyLengthException {
        UUID uuid = UUID.randomUUID();
        dictionaryValue.setId(uuid.toString());


        HashMap<String, Object> map = new HashMap<>();

        if (dictionaryValue.getValue().length() > 20) {
            throw new MyLengthException("名称的长度不能大于20");
        }

        if (dictionaryValue.getText().length() > 100) {
            throw new MyLengthException("文本的长度不能大于100");
        }

        try {
            int add = dictionaryValueServices.add(dictionaryValue);
            if (add > 0) {
                map.put("result", "success");
            } else {
                throw new SQLException("由于业务逻辑错误，则数据库添加影响行数异常");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            throw new SQLException("由于SQL语句错误，则我们需要再次抛出一个异常交给增强控制器，数据库删除影响行数异常");
        }
        return map;
    }


    @RequestMapping("editBefore.action")
    public DictionaryValue editBefore(String id) throws SQLException, MyLengthException {
        DictionaryValue dictionaryValue = dictionaryValueServices.get(id);
        System.out.println("editBefore:"+dictionaryValue.getTypeCode());
        return dictionaryValue;
    }


    @RequestMapping("edit.action")
    public HashMap<String, Object> edit(DictionaryValue dictionaryValue) throws SQLException, MyLengthException {
        HashMap<String, Object> map = new HashMap<>();

        if (dictionaryValue.getValue().length() > 20) {
            throw new MyLengthException("名称的长度不能大于20");
        }

        if (dictionaryValue.getText().length() > 100) {
            throw new MyLengthException("文本的长度不能大于100");
        }






        try {
            int edit = dictionaryValueServices.edit(dictionaryValue);
            if (edit > 0) {
                map.put("result", "success");
            } else {
                throw new SQLException("由于业务逻辑错误，则数据库添加影响行数异常");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            throw new SQLException("由于SQL语句错误，则我们需要再次抛出一个异常交给增强控制器，数据库删除影响行数异常");
        }
        return map;
    }


}
